package com.dkt.android.mvp.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.android.library.YLCircleImageView
import com.bumptech.glide.Glide
import com.dkt.android.R
import com.dkt.android.base.BaseHolder
import com.dkt.android.img.loadImgUrlWithManager
import com.dkt.android.mvp.model.IndexShopBean

/**
 * @author: jcm
 * @email: jiangcm@aplus-it.cn
 * @createTime: 20-6-3
 */
class IndexShopAdapter(
    private val list: MutableList<IndexShopBean>,
    private val mLayoutHelper: LayoutHelper,
    private val type: Int = 1
) : DelegateAdapter.Adapter<BaseHolder>() {

    var context: Context? = null

    private var listener: ((position: Int,type:Int,str:String) -> Unit?)? = null

    fun setOnListener(listener: (position: Int,type:Int,str:String) -> Unit?) {
        this.listener = listener
    }


    override fun getItemCount(): Int = if (type != 1) {
        1
    } else {
        list.size
    }

    override fun onBindViewHolder(holder: BaseHolder, position: Int) {
        when (type) {
            1 -> {
                val data = list[position]
                holder.getView<YLCircleImageView>(R.id.imgYLC)?.loadImgUrlWithManager(
                    Glide.with(context!!),
                    data.img,
                    false
                )
                holder.setText(R.id.name, data.name)
                holder.setText(R.id.fans, data.fans)
                holder.setText(R.id.desc, data.desc)
                holder.setText(R.id.detils, data.detils)
                holder.setOnClickListener(R.id.index_layout) {
                    listener?.invoke(position,type,"")
                }
            }
            else -> {
                holder.setOnClickListener(R.id.select) {
                    listener?.invoke(position,type,holder.getView<EditText>(R.id.key)?.text.toString())
                }
            }
        }

    }

    override fun getItemViewType(position: Int) = type

    /**
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHolder {
        if (context == null) {
            context = parent.context
        }
        return when (viewType) {
            1 -> BaseHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_index_shop, parent, false)
            )
            else -> BaseHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_select, parent, false)
            )
        }
    }

    override fun onCreateLayoutHelper(): LayoutHelper = mLayoutHelper

}